package com.extension.company.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.extension.company.admin.R;
import com.extension.company.model.api.RetrofitUrlApi;
import com.extension.company.model.entity.ShopCarData;
import com.extension.company.common.widget.CartNumCalcultor;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 功能：购物车 recycleView 适配器
 */
public class FragmentCarAdapter extends BaseRecyclerAdapter<ShopCarData, RecyclerView.ViewHolder> {


	private ItemCheckListener mItemCheckListener;

	public FragmentCarAdapter(Context context, List<ShopCarData> list) {
		super(context, list);
	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		LayoutInflater mInflater = LayoutInflater.from(mContext);
		RecyclerView.ViewHolder viewHolder = null;
		switch (viewType) {
			case 0:           // 店铺
				View storeView = mInflater.inflate(R.layout.fragmentcar_store_item, parent, false);
				viewHolder = new StoreHolder(storeView);
				break;
			case 1:          // 商品
				View commodityView = mInflater.inflate(R.layout.fragmentcar_item, parent, false);
				viewHolder = new GoodsHolder(commodityView);
				break;
			default:
				break;
		}
		return viewHolder;
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
		if (viewHolder instanceof GoodsHolder) { // 商品设置数据
			final GoodsHolder mGoodHolder = (GoodsHolder) viewHolder;
			mGoodHolder.carCommodityDetail.setText(list.get(position).getCommodityDetail());
			mGoodHolder.carCommodityPrice.setText(list.get(position).getCommodityPrice());
			mGoodHolder.carCommodityName.setText(list.get(position).getCommodityName());
			mGoodHolder.numCalcultor.setValue(Integer.valueOf(list.get(position).getCommodityNumber()));
			mGoodHolder.commodityCheckBox.setChecked(list.get(position).isChecked());
			Glide.with(mContext).load(RetrofitUrlApi.BaseHostUrl + list.get(position).getCommidityImg()).into(mGoodHolder.carGoodsImg);
			mGoodHolder.numCalcultor.setOnNumCalculatorClickListener(new CartNumCalcultor.OnNumCalculatorClickListener() { // 购物车中商品数量加减操作
				@Override
				public void onAddClick(View view, int value) { //数量加
					mGoodHolder.numCalcultor.setValue(value);
					list.get(position).setCommodityNumber(value + "");
					mItemCheckListener.onCalculateChange(list);
				}

				@Override
				public void onCutClick(View view, int value) { // 数量减
					mGoodHolder.numCalcultor.setValue(value);
					list.get(position).setCommodityNumber(value + "");
					mItemCheckListener.onCalculateChange(list);
				}
			});

			mGoodHolder.commodityCheckBox.setOnClickListener(new View.OnClickListener() {  // 商品  选中监听事件
				@Override
				public void onClick(View v) {
					commodityCheckChange(position, ((CheckBox) v).isChecked());

				}
			});

		} else if (viewHolder instanceof StoreHolder) {  // 店铺的item设置数据
			final StoreHolder mStoreHolder = (StoreHolder) viewHolder;
			mStoreHolder.storeCheck.setChecked(list.get(position).isChecked());
			Glide.with(mContext).load(RetrofitUrlApi.BaseHostUrl + list.get(position).getStoreImg()).into(mStoreHolder.storeImg);
			mStoreHolder.storeName.setText(list.get(position).getStoreName());
			mStoreHolder.storeCheck.setOnClickListener(new View.OnClickListener() { // 店铺选择
				@Override
				public void onClick(View v) {
					storeCheckChange(position, ((CheckBox) v).isChecked(), list.get(position).getItemType());
				}
			});
		}


	}

	@Override
	public int getItemViewType(int position) {
		return list.get(position).getItemType();
	}

	/**
	 * 全选
	 *
	 * @param isChecked
	 */
	public void allSelect(Boolean isChecked) {

		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).isChecked() != isChecked) {
				list.get(i).setChecked(isChecked);
			}
		}
		notifyDataSetChanged();
		mItemCheckListener.onCalculateChange(list);

	}


	/**
	 * 商品的viewHolder
	 */
	class GoodsHolder extends RecyclerView.ViewHolder {

		@BindView(R.id.car_commodity_name)
		TextView carCommodityName;
		@BindView(R.id.car_commodity_price)
		TextView carCommodityPrice;
		@BindView(R.id.car_commodity_detail)
		TextView carCommodityDetail;
		@BindView(R.id.calcutor_num)
		CartNumCalcultor numCalcultor;
		@BindView(R.id.commodity_checkbox)
		CheckBox commodityCheckBox;
		@BindView(R.id.car_goods_img)
		ImageView carGoodsImg;

		public GoodsHolder(@NonNull View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
		}
	}


	/**
	 * 店铺的viewHolder
	 */
	class StoreHolder extends RecyclerView.ViewHolder {
		@BindView(R.id.car_store_check)
		CheckBox storeCheck;
		@BindView(R.id.car_store_img)
		ImageView storeImg;
		@BindView(R.id.car_store_name)
		TextView storeName;

		public StoreHolder(@NonNull View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
		}
	}

	public interface ItemCheckListener {
		void onCalculateChange(List<ShopCarData> mData);
	}

	public void setItemCheckListener(ItemCheckListener listener) {
		this.mItemCheckListener = listener;
	}

	/**
	 * 商品的选中
	 *
	 * @param position  当前的商品所在的位置
	 * @param isChecked
	 */
	private void commodityCheckChange(int position, boolean isChecked) {
		list.get(position).setChecked(isChecked); // 商品选中，更改list数据中的商品的状态
		notifyItemChanged(position, "0x001");
		mItemCheckListener.onCalculateChange(list); // 将商品list数据返给购物车界面，进行价格计算
	}

	/**
	 * @param position  点击的展示数据在列表中的位置
	 * @param isChecked 点击的手，设置的选中还是不选中
	 * @param itemType  item的类型
	 */
	private void storeCheckChange(int position, boolean isChecked, int itemType) {
		list.get(position).setChecked(isChecked); // 将当前的商铺设置为选中状态
		for (int i = position + 1; i < list.size(); i++) {
			if (list.get(i).getItemType() == itemType) {
				break;
			} else if (list.get(i).getItemType() == 1) { // 商品
				if (list.get(i).isChecked() != isChecked) {
					list.get(i).setChecked(isChecked);
					notifyItemChanged(i, "0x002");
				}
			}
		}
		mItemCheckListener.onCalculateChange(list);

	}

}
